﻿@using System.Drawing
@model StackExchange.Opserver.Views.Exceptions.ExceptionsModel
@{
    Layout = null;
    var showActions = Current.User.IsExceptionAdmin;
}
@functions {
    public IHtmlString ErrorStyle(StackExchange.Exceptional.Error e)
    {
        var ageHours = (DateTime.UtcNow - e.CreationDate).TotalHours;
        if (ageHours >= 1) return null;
        var uncapped = Math.Sqrt(e.DuplicateCount ?? 1) / Math.Pow((ageHours + 1), 2);
        var color = Color.FromArgb((int)(255 * Math.Min(1, uncapped)), 0, 0);
        return ("color:" + ColorTranslator.ToHtml(color) + ";").AsHtml();
    }

    public string GetShortName(StackExchange.Exceptional.Error e)
    {
        const string trimEnd = "Exception";
        var lastName = e.Type.Split(StringSplits.Period_Plus).Last();
        if (lastName != trimEnd && lastName.EndsWith(trimEnd))
        {
            return lastName.Substring(0, lastName.Length - trimEnd.Length);
        }
        return lastName;
    }
}
@foreach (var e in Model.Errors)
{
    var errorStyle = ErrorStyle(e);
    <tr class="error@(e.IsProtected ? " protected" : "")@(e.DeletionDate.HasValue ? " deleted" : "")" data-id="@e.GUID" data-log="@e.ApplicationName">
        <td class="actions@(showActions ? "" : " hidden")">
            <a title="@(e.DeletionDate.HasValue ? "Error is already deleted" : "Delete this error")" class="delete-link" href="/exceptions/delete">&nbsp;X&nbsp;</a>
            @if (e.DeletionDate.HasValue)
            {
                <a title="Undelete and protect this error" class="protect-link" href="/exceptions/protect">&nbsp;P&nbsp;</a>
            }
            else if (!e.IsProtected)
            {
                <a title="Protect this error from automatic deletion" class="protect-link" href="/exceptions/protect">&nbsp;P&nbsp;</a>
            }
            else
            {
                <span title="This error is protected" class="protected">&nbsp;O&nbsp;</span>
            }
        </td>
        <td style="@errorStyle" data-val="@e.CreationDate.ToEpochTime()">@e.CreationDate.ToRelativeTimeSpan()</td>
        @if (Model.ShowAll)
        {
            <td title="@e.Type"><a class="note" href="/exceptions?log=@e.ApplicationName.UrlEncode()">@e.ApplicationName</a></td>
        }
        else
        {
            <td class="note" title="@e.Type">@GetShortName(e)</td>
        }
        <td class="text-cell exception-cell">
            <div class="overflow-cap-500">
                <a class="exception-link@(e.Message.StartsWith("Failed to retrieve error log:") ? " bad-error" : "")" href="/exceptions/detail?id=@e.GUID&app=@e.ApplicationName.UrlEncode()">@(e.Message.TruncateWithEllipsis(110))</a>
            </div>
        </td>
        <td class="text-cell" title="@e.Url">
            <div class="overflow-cap-150">@e.Url.TruncateWithEllipsis(40)</div>
        </td>
        <td>@e.IPAddress</td>
        <td>@e.Host</td>
        <td style="@errorStyle">@e.MachineName.ToLower()</td>
        <td style="@errorStyle" data-count="@e.DuplicateCount">@Html.Raw(e.DuplicateCount > 1 ? "(" + (e.DuplicateCount) + "&times;)" : "")</td>
    </tr>
}   